package org.agilelang.editor.syntaxtext.dialogpopup;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import org.agilelang.object.Controller;
import org.agilelang.object.Menu;
import org.agilelang.object.Project;
import org.agilelang.object.attributes.View;


public class DlgMenuAddMenuItem extends DialogBase {

    JComboBox name_menu = new JComboBox();
    JTextField name = new JTextField(25);
    JTextField caption = new JTextField(25);
    JComboBox controller = new JComboBox();
    JComboBox action = new JComboBox();
    JTextField description = new JTextField(25);
    Project project;

    public DlgMenuAddMenuItem(Project project) {
        super("Menu : addMenuItem()", 7);
        this.project = project;
        initComponents();
        initUI();
    }

    private void initComponents() {

        List<Menu> listm = project.menus;
        int t = listm.size();
        for (int i = 0; i < t; i++) {
            Menu menu = listm.get(i);
            name_menu.addItem(menu.name);
        }
        //----------------------------------------------------------------------
        List<Controller> listc = project.controllers;
        t = listc.size();
        for (int i = 0; i < t; i++) {
            Controller co = listc.get(i);
            controller.addItem(co.name);
        }
        /*List<Scaffold> lists = project.scaffolds;
        t = lists.size();
        for (int i = 0; i < t; i++) {
            Scaffold scaffold = lists.get(i);
            controller.addItem(scaffold.name);
        }*/
        //----------------------------------------------------------------------
        controller.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                System.out.println(e.getItem());

                action.removeAllItems();

                List<Controller> listc = project.controllers;
                //List<Scaffold> lists = project.scaffolds;
                int t = listc.size();
                //int v = lists.size();
                for (int i = 0; i < t; i++) {
                    Controller co = listc.get(i);
                    if (e.getItem().toString().endsWith(co.name)) {
                        List<View> lista = co.views;
                        int w = lista.size();
                        for (int j = 0; j < w; j++) {
                            View attr = lista.get(j);
                            action.addItem(attr.name);
                            return;
                        }
                    }
                }
                /*for (int i = 0; i < v; i++) {
                    Scaffold scaffold = lists.get(i);
                    if (e.getItem().toString().endsWith(scaffold.name)) {
                        List<View> lista = scaffold.views;
                        int w = lista.size();
                        action.addItem("crud");
                        for (int j = 0; j < w; j++) {
                            View attr = lista.get(j);
                            action.addItem(attr.name);
                            return;
                        }
                    }
                }*/
            }
        });
        //----------------------------------------------------------------------

        this.addLineTxt("Menu", name_menu);
        this.addSapce();
        this.addLineTxt("name", name);
        this.addLineTxt("caption", caption);
        this.addLineTxt("controller", controller);
        this.addLineTxt("action", action);
        this.addLineTxt("description", description);
    }

    public String getSentences() {
        return "Menu." + name_menu.getSelectedItem().toString() + ".addMenuItem(\n"
                + "  name = \"" + name.getText() + "\",\n"
                + "  caption = \"" + caption.getText() + "\",\n"
                + "  controller = \"" + controller.getSelectedItem().toString() + "\",\n"
                + "  action = \"" + action.getSelectedItem().toString() + "\",\n"
                + "  description = \"" + description.getText() + "\" );\n";
    }
}